<?php 

namespace app\controller\bossManager;

use app\exception\ErrnoPlus;
use app\service\SendTemplateRelationService;
use app\service\SendTemplateService;
use app\validate\PageValidate;
use app\validate\SendTemplateRelationValidate;
use share\service\SendCodeService;
use share\utils\Helper;

class SendTemplateRelationController extends WebBossController{

    public function initValidate(){
        $this->validate = new SendTemplateRelationValidate();
    }

    public function list_action(){
        $param = $this->request->param();        
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $data = SendTemplateRelationService::getListInfo($param,$page);

  
        $templateInfos = SendTemplateService::getAllByIds(array_column($data,'send_template_id'));
   
        foreach($data as &$item){
            $template = Helper::filterOne($templateInfos,['id'=>$item['send_template_id']]);
            $item['send_config_id'] = 0;
            $item['type'] = 0;
            $item['language'] = 0;
            $item['no'] = '';
            $item['scene'] = '';
            $item['name'] = '';
            $item['title'] = '';
            if($template){
                $item['send_config_id'] = $template['send_config_id'];
                $item['type'] = $template['type'];
                $item['language'] = $template['language'];
                $item['no'] = $template['no'];
                $item['scene'] = $template['scene'];
                $item['name'] = $template['name'];
                $item['title'] = $template['title'];
            }
        }
        $total = SendTemplateRelationService::getListCountInfo($param);
        return self::ok(self::formatPageData($page,$total,$data));
    }

    public function get_by_app_action(){
        $input = $this->check();
        $res = SendTemplateRelationService::getAllByFilter([
            'other_id' => $input['other_id'],
            'other_type' => $input['other_type'],
        ]);
        return self::ok($res);
    }

    public function set_action(){
        $input = $this->check();
        $res = SendTemplateRelationService::setRelation($input);
        return self::ok($res);
    }

    public function check_action(){
        $input = $this->check();
        $res = SendTemplateRelationService::getBestMatchRelation(
            $input['vendor'],
            $input['package'],
            intval($input['language']),
            intval($input['type']),
            $input['scene']
        );
        return self::ok($res);
    }

    public function send_test_action(){
        $input = $this->check();
        $res = SendTemplateRelationService::getBestMatchRelation(
            $input['vendor'],
            $input['package'],
            intval($input['language']),
            intval($input['type']),
            $input['scene']
        );
        if(empty($res)){
            return self::fail("未找到合适的发送模版",ErrnoPlus::COMMON_ERROR);
        }
        $replaceInfo = [];
        if(!empty($res['params'])){            
            $keys = explode(',',$res['params']);
            foreach($keys as $key){
                $replaceInfo[$key] = '123456';
            }
        }
        $account = $input['account'];
        if($res['type'] == SendTemplateService::SEND_TYPE_OF_SMS){
            SendCodeService::sendSMSBySendTemplateDetail($account,$replaceInfo,$res);
        }
        else{
            SendCodeService::sendEmailBySendTemplateDetail($account,$replaceInfo,$res);
        }
        
        return self::ok($input);
    }

    public function del_action(){
        $input = $this->check();
        $res = SendTemplateRelationService::del($input['id']);
        return self::ok($res);
    }
}
